<?php

/*
 * Action       +$3
 * 
 * Put a card from your hand on top of your deck.
 * 
 * When you gain this, put all Treasures you have in play on top of your deck in any order.
 * 
 * TODO: check what happens if you gain this card but not in the buy phase
 */

class Mandarin extends Card implements iKingdom, iHinterlands, iAction {

    public function __construct() {
        parent::__construct("Mandarin", 5);
    }

    public function getDescription() {
        return array(
            "+$3.",
            "Put a card from your hand on top of your deck.",
            "When you gain this, put all Treasures you have in play on top of your deck in any order.");
    }

    public function play(Game $game, $isRealCard = true) {
        $game->addMoney(new Price(3));
        ScreenDisplay::addScreenMessage($game, true, "get +3$", "gets +3$");

        $playHand = $game->getPlayHand();
        if (sizeof($playHand) > 0) {
            $playHand = getUniqueCards($playHand);
            if (sizeof($playHand) > 1) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $playHand, 1, 1, null, "Choose a card to return to draw deck");
                $card_name = $output[0];
            }
            else {
                $card_name = $playHand[0];
            }
            $card = $game->moveCardFromPileToPile($card_name, PlayHand, DrawPile);
            ScreenDisplay::addScreenMessage($game, false, " and return " . nicePrint($card) . " on your deck", " and returns " . sizeof($card) . " cards back to his deck");
        }
    }

    public function gainThisCard(Game $game, $name = null) {
        $playArea = $game->getPlayArea($name);
        $treasures = array();
        foreach ($playArea as $card) {
            if ($card instanceof iTreasure) {
                $treasures[] = $card;
            }
        }

        if (sizeof($treasures) > 0) {
            $output = MySocket::GetUserReply($game, Input_Reorder, $treasures, -1, -1, null, "Put cards on top of deck in any order", $name);
            ScreenDisplay::addScreenMessage($game, true, "put " . nicePrint($output) . " back to the deck", "puts " . sizeof($output) . " cards back on his deck", $name);
            $output = array_reverse($output);
            foreach ($output as $p) {
                $game->putCardOnTopOfDrawDeck($p, $name);
            }
        }

        $game->setTempCard($this);
        return true;
    }

}

?>
